package srm510;

import java.util.HashSet;

public class TheLuckyBasesDivTwo {
    public long find(long n) {
        if (n == 4 || n == 7)
            return -1;

        HashSet<Long> set = new HashSet<Long>();
        boolean lucky;
        long num;
        for (int b = 2; b < 1000000; b++) {
            lucky = true;
            num = n;
            while (num > 0) {
                if (num % b != 7 && num % b != 4) {
                    lucky = false;
                    break;
                }
                num /= b;
            }
            if (lucky)
                set.add((long) b);
        }
        
        for (int i = 4; i <= 7; i += 3) {
            for (int j = 4; j <= 7; j += 3) {
                if ((n - j) % i != 0) continue;
                Long d = (n - j) / i;
                if (d > i && d > j)
                    set.add(d);
            }
        }
        return set.size();
    }

    // BEGIN KAWIGIEDIT TESTING
    // Generated by KawigiEdit 2.1.8 (beta) modified by pivanof
    private static boolean KawigiEdit_RunTest(int testNum, long p0, boolean hasAnswer, long p1) {
        System.out.print("Test " + testNum + ": [" + p0);
        System.out.println("]");
        TheLuckyBasesDivTwo obj;
        long answer;
        obj = new TheLuckyBasesDivTwo();
        long startTime = System.currentTimeMillis();
        answer = obj.find(p0);
        long endTime = System.currentTimeMillis();
        boolean res;
        res = true;
        System.out.println("Time: " + (endTime - startTime) / 1000.0 + " seconds");
        if (hasAnswer) {
            System.out.println("Desired answer:");
            System.out.println("\t" + p1);
        }
        System.out.println("Your answer:");
        System.out.println("\t" + answer);
        if (hasAnswer) {
            res = answer == p1;
        }
        if (!res) {
            System.out.println("DOESN'T MATCH!!!!");
        } else if ((endTime - startTime) / 1000.0 >= 2) {
            System.out.println("FAIL the timeout");
            res = false;
        } else if (hasAnswer) {
            System.out.println("Match :-)");
        } else {
            System.out.println("OK, but is it right?");
        }
        System.out.println("");
        return res;
    }

    public static void main(String[] args) {
        boolean all_right;
        all_right = true;

        long p0;
        long p1;

        // ----- test 0 -----
        p0 = 255L;
        p1 = 1L;
        all_right = KawigiEdit_RunTest(0, p0, true, p1) && all_right;
        // ------------------

        // ----- test 1 -----
        p0 = 4L;
        p1 = -1L;
        all_right = KawigiEdit_RunTest(1, p0, true, p1) && all_right;
        // ------------------

        // ----- test 2 -----
        p0 = 13L;
        p1 = 0L;
        all_right = KawigiEdit_RunTest(2, p0, true, p1) && all_right;
        // ------------------

        // ----- test 3 -----
        p0 = 60L;
        p1 = 2L;
        all_right = KawigiEdit_RunTest(3, p0, true, p1) && all_right;
        // ------------------

        if (all_right) {
            System.out.println("You're a stud (at least on the example cases)!");
        } else {
            System.out.println("Some of the test cases had errors.");
        }
    }
    // END KAWIGIEDIT TESTING
}
//Powered by KawigiEdit 2.1.8 (beta) modified by pivanof!
